

/** JFlex lexical analyser for simple calculator Calc.
 *
 * The input to JFlex consists of three sections:
 * - user code to be included directly in the generated class,
 * - options and definitions for JFlex, and
 * - lexical rules defining the tokens.
 * These sections are separated by lines containing "%%"
 */

/* --------------------------Usercode Section------------------------ */

import java_cup.runtime.*;

%%
/* -----------------Options and Declarations Section----------------- */
/* The name of the class JFlex will create will be Lexer.
 * Will write the code to the file Lexer.java.
 */
%class lexer

/* Will switch to a CUP compatibility mode to interface with a CUP
 * generated parser.
 * The terminal symbols defined by CUP are placed in the class sym.
 */
%cup

/* Macro Declarations
 * These declarations are regular expressions that will be used latter
 * in the Lexical Rules Section.
 */

/* A line terminator is a \r (carriage return), \n (line feed), or \r\n. */
LineTerminator = \r|\n|\r\n

/* White space is a line terminator, space, tab, or line feed. */
WhiteSpace = [ \t\f]

Digit	= [0-9]

%%
/* ------------------------Lexical Rules Section---------------------- */

/* This section contains regular expressions and actions, i.e., Java
 * code, that will be executed when the scanner matches the associated
 * regular expression. */

"+"	      { return new Symbol( sym.PLUS ); }
"-"	      { return new Symbol( sym.MINUS ); }
"*"	      { return new Symbol( sym.TIMES ); }
"("	      { return new Symbol( sym.LPAREN ); }
")"	      { return new Symbol( sym.RPAREN ); }
{LineTerminator}
              { return new Symbol( sym.NEWLINE ); }
{WhiteSpace}  { /* ignore white space */ }
{Digit}+      { int value = 0x80808080; // Nonsense value
                try {
                    value = Integer.parseInt( yytext() );
                } catch( NumberFormatException e ) { 
                    /* Can only happen if the number is too big */
                    System.out.println( "integer too large" );
                    return new Symbol( sym.ILLEGAL );
				}
                return new Symbol( sym.NUMBER, new Integer( value) );
              }
.             { return new Symbol( sym.ILLEGAL ); }

